package com.shengyin.ec.model.dao.order;

import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.shengyin.ec.model.dao.BaseDaoImpl;
import com.shengyin.ec.model.entity.Goods;
import com.shengyin.ec.model.entity.News;
import com.shengyin.ec.model.entity.Orders;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.model.entity.Receiver;

@Repository("orderInfoDao")
public class OrderDaoImpl extends BaseDaoImpl<Orders> implements OrderDao {
	
	private static int orderNum = 0001;
		
	public String generateOrderId(){
		return getDate();
	}
	
	public String getDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String today = sdf.format(new Date());
		Long dateL = Long.parseLong(today)*1000;
		dateL += getOrderNum();
		return dateL.toString();
	}
	
	public static int getOrderNum(){
		orderNum++;
		return orderNum;
	}
	
	
	public void deleteSelectedGoods(Long[] ids){
		Orders orders=null;
		List<Orders> OrdersList=new ArrayList<Orders>(0);
		for(long id:ids)
		{
			orders=super.get(id);
			OrdersList.add(orders);
		}
		super.deleteAll(OrdersList);
	}
	
	public void updateState(Orders order){
	  super.update(order);
	}
	
	@SuppressWarnings("unchecked")
	public void deleteRelateOrder(String orderId){
		List<Orders> orders = new ArrayList<Orders>(0);
		orders = super.find("from Orders o where o.orderId='"+orderId+"'");
		super.deleteAll(orders);
	}
	
	public List<Orders> getOrdersByUserId(long id){
		return super.find("from Orders o where o.user.id='"+id+"'");
	}
	
	//orders about cluster
	
    public List<Orders> getClusterCompanyOrdersList(long clusterCompanyid,int offset,int pageSize){
    	
    	return super.findByPage("from Orders where clusterCompany_id=?", clusterCompanyid, offset, pageSize);
    }
	
	public List<Orders> getClusterCompanyOrdersList(long clusterCompanyid){
		
		return super.find("from Orders where clusterCompany_id='"+clusterCompanyid+"'");
	}
	
    public List<Orders> getOrderByOrderId(String orderId,int offset,int pageSize){
    	
    	return super.findByPage("from Orders where orderId like ?", '%'+orderId+'%', offset, pageSize);
    }
	
	public List<Orders> getOrderByOrderId(String orderId){
		
		return super.find("from Orders where orderId like %'"+orderId+"'%");
	}
	
    public List<Orders> getOrderByStat(String[] paramValues,int offset,int pageSize){
    	
    	return super.findByPage("from Orders where clusterCompany_id = ? and orderStat = ?", paramValues, offset, pageSize);
    }
	
	public List<Orders> getOrderByStat(String[] paramValues){
		
		return super.find("from Orders where clusterCompany_id = ? and orderStat = ?", paramValues);
	}
	
    public List<Orders> getOrderByDate(String[] paramValues,int offset,int pageSize){
    	
    	return super.findByPage("from Orders where clusterCompany_id = ? and datediff(d,createDate,getDate())<= ?", paramValues, offset, pageSize);
    }
	
	public List<Orders> getOrderByDate(String[] paramValues){
		
		return super.find("from Orders where clusterCompany_id = ? and datediff(d,createDate,getDate())<= ?",paramValues);
	}
	
	//orders about shengyin
	
    public List<Orders> getAllOrder(int type,int offset,int pageSize){
    	
    	if(type==0)
    	  return super.findByPage("from Orders", offset, pageSize);
    	else
    	  return super.findByPage("from Orders where clusterCompany_id='圣茵自营'",offset,pageSize);	
    }
	
	public List<Orders> getAllOrder(int type){
		
		if(type==0)
	    	  return super.find("from Orders");
	    	else
	    	  return super.find("from Orders where clusterCompany_id='圣茵自营'");
	}
	
    public List<Orders> getOrderByStat(String orderStat,int offset,int pageSize){
    	
    	return super.findByPage("from Orders where orderStat='"+orderStat+"'", offset, pageSize);
    }
	
	public List<Orders> getOrderByStat(String orderStat){
		
		return super.find("from Orders where orderStat='"+orderStat+"'");
	}
	
   public List<Orders> getOrderByDate(String date,int offset ,int pageSize){
	   
	   return super.findByPage("from Orders where datediff(d,createDate,getDate())<= '"+date+"'", offset, pageSize);
   }
	
	public List<Orders> getOrderByDate(String date){
		
	   return super.find("from Orders where datediff(d,createDate,getDate())<= '"+date+"'");
	}
}
